/**
 * 数据字典router
 */
import express from "express";
import { Schema } from "mongoose";
import {
  addData,
  findAll,
  updateData,
  deleteData,
  getValueData,
} from "../mdb/controlDB";
import {
  baseResp,
  dictLabelSchematype,
  dictLabelModalname,
  dictValueSchematype,
  dictValueModalname,
} from "../common";

const router = express.Router();

const dictlabelschema = new Schema(dictLabelSchematype);

const dictValueschema = new Schema(dictValueSchematype);

interface ITDictLabelItem {
  _id: any;
  key: string;
  desc: string;
}

// router.get("/", async function (req, res) {
//   res.json({ status: 1 });
// });

// 新增数据
router.post("/addlabel", function (req, res) {
  const param = req.body;
  const addback = (data: any) => {
    res.json(Object.assign({}, baseResp));
  };

  addData({
    schematype: dictlabelschema,
    callback: addback,
    modalname: dictLabelModalname,
    param,
  });
});

router.get("/alllabel", async function (req, res) {
  const findBack = (data: ITDictLabelItem[]) => {
    let list: any[] = [];
    if (data && data.length) {
      data.forEach((item) => {
        const { _id, key, desc } = item;
        list.push({
          id: _id.toString(),
          key,
          desc,
        });
      });
    }
    res.json(Object.assign({}, baseResp, { data: list }));
  };
  findAll({
    schematype: dictlabelschema,
    callback: findBack,
    modalname: dictLabelModalname,
  });
});

// 删除数据
router.post("/dellabel", (req, res) => {
  const param = req.body;

  const delback = (data: any) => {
    res.json(Object.assign({}, baseResp, { data }));
  };
  deleteData({
    schematype: dictlabelschema,
    callback: delback,
    modalname: dictLabelModalname,
    param,
  });
});

// 修改数据
router.post("/updatelabel", (req, res) => {
  const param = req.body;

  const editback = (data: any) => {
    res.json(Object.assign({}, baseResp, { data }));
  };
  updateData({
    schematype: dictlabelschema,
    callback: editback,
    modalname: dictLabelModalname,
    param,
  });
});

// router.post("/createdbs", async function (req, res) {
//   await createMDB({
//     schematype: langschema,
//     modalname: langModalname,
//   });
//   res.json(baseResp);
// });

// 新增字典值
router.post("/addvalue", function (req, res) {
  const param = req.body;
  const addback = (data: any) => {
    res.json(Object.assign({}, baseResp));
  };

  addData({
    schematype: dictValueschema,
    callback: addback,
    modalname: dictValueModalname,
    param,
  });
});

// 根据id请求所有字典值
router.post("/values", function (req, res) {
  const param = req.body;
  const callback = (data: any) => {
    let list: any[] = [];
    if (data && data.length) {
      data.forEach((item: any) => {
        const { _id, pid, label, cnlabel, value, desc, sort } = item;
        list.push({
          id: _id.toString(),
          pid,
          desc,
          label,
          cnlabel,
          value,
          sort,
        });
      });
    }
    res.json(Object.assign({}, baseResp, { data: list }));
  };

  getValueData({
    schematype: dictValueschema,
    callback,
    modalname: dictValueModalname,
    param,
  });
});

// 删除字典值数据
router.post("/delvalue", (req, res) => {
  const param = req.body;

  const delback = (data: any) => {
    res.json(Object.assign({}, baseResp, { data }));
  };
  deleteData({
    schematype: dictValueschema,
    callback: delback,
    modalname: dictValueModalname,
    param,
  });
});

router.post("/updatevalue", (req, res) => {
  const param = req.body;

  const editback = (data: any) => {
    res.json(Object.assign({}, baseResp, { data }));
  };
  updateData({
    schematype: dictValueschema,
    callback: editback,
    modalname: dictValueModalname,
    param,
  });
});

router.get("/fulldict", async function (req, res) {
  const findBack = (data: ITDictLabelItem[]) => {
    let list: any[] = [];
    if (data && data.length) {
      data.forEach((item, index) => {
        const { _id, key, desc } = item;

        const getValueBack = (vdata: any) => {
          let valuelist: any[] = [];
          if (vdata && vdata.length) {
            vdata.forEach((item: any) => {
              const { _id, pid, label, cnlabel, value, desc, sort } = item;
              valuelist.push({
                id: _id.toString(),
                pid,
                desc,
                label,
                cnlabel,
                value,
                sort,
              });
            });
          }
          list.push({
            id: _id.toString(),
            key,
            desc,
            values: valuelist,
          });

          if (index === data.length - 1) {
            res.json(Object.assign({}, baseResp, { data: list }));
          }
        };

        getValueData({
          schematype: dictValueschema,
          callback: getValueBack,
          modalname: dictValueModalname,
          param: {
            id: _id,
          },
        });
      });
    } else {
      res.json(Object.assign({}, baseResp));
    }
  };

  findAll({
    schematype: dictlabelschema,
    callback: findBack,
    modalname: dictLabelModalname,
  });
});

export default router;
